<?php

namespace app\admin\controller\store;

use app\admin\model\freight\ExpressDelivery as ExpressDeliveryModel;
use app\admin\model\store\Goods as GoodesModel;
use app\admin\model\store\GoodsSku;
use app\common\controller\AdminController;
use think\App;
use think\facade\View;

/**
 * @ControllerAnnotation(title="pink_config")
 */
class PinkConfig extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\store\PinkConfig();
    }

    //渲染商品添加页面
    public function goodsAdd()
    {
        return View::fetch();
    }

    //渲染商品添加列表数据  layui数据表格
    public function goodsList()
    {
        $goodsInfo = GoodesModel::with('goodsCate')->paginate($this->pagesize);
        if (empty($goodsInfo)) {
            return json(['code' => 0, 'msg' => '失败', 'data' => '']);
        } else {
            $goodsInfo = $goodsInfo->toArray();
            return json(['code' => 0, 'msg' => '成功', 'count' => $goodsInfo['total'], 'data' => $goodsInfo['data']]);
        }
    }

    //拼团添加表单
    public function pinkAdd()
    {
        View::engine()->layout(false);
        $goodsId = $this->request->post('goods_id');
        //获取商品的信息 商品sku的信息
        $goodsInfo = [];
        $goodsInfo = GoodesModel::with('sku')->field(['id', 'goods_name', 'image', 'goods_carousel'])->find($goodsId);
        //组装轮播图数据
        $goodsInfo['goods_carousel'] = !empty($goodsInfo['goods_carousel']) ? explode('|', $goodsInfo['goods_carousel']) : [];
        $goodsInfo && $goodsInfo = $goodsInfo->toArray();
        //查询运费模板的信息
        $freight = ExpressDeliveryModel::field(['id', 'template_name'])->select();
        //如果有数据转成数组 没有数据 为空数组
        $freight && $freight = $freight->toArray();
        View::assign(['goods_info' => $goodsInfo, 'freight' => $freight]);
        return View::fetch();
    }

    //拼团添加保存
    public function pinkAddSave()
    {
        $post = $this->request->post();
        $rule = [
            'goods_sku_id' => 'require',//商品sku_id
            'goods_name' => 'require',//商品名称
            'goods_img' => 'require',//拼团活动和商品图片
            'pink_desc' => 'require',//拼团活动简介
            'pink_unit' => 'require|number',//拼团单位
            'pink_time' => 'require',//拼团时间
            'validity_time' => 'require|number',//拼团有效时常
            'pink_people' => 'require|number',//拼团人数
            'pink_status' => 'require|number',//拼团状态
            'freight_id' => 'require|number',//运费模板id
            'pink_pay_num' => 'require|number',//商品购买数量上限
            'pink_price' => 'require',//商品规格价钱
            'pink_sku_stock' => 'require|number',//拼团活动库存
            'goods_sku_price' => 'require',//原价
        ];

        $time = explode(' 到 ', $post['pink_time']);
        $post ['start_time'] = strtotime($time[0]);
        $post ['end_time'] = strtotime($time[1]);
        $post['goods_carouse'] = implode('—', $post['goods_carouse']);
        $this->validate($post, $rule);
        try {
            //查询商品sku库存
            $skuNum = GoodsSku::where('id', $post['goods_sku_id'])->value('sku_stock');
            //计算库存
            $skuNum = $skuNum - $post['pink_sku_stock'];
            //修改sku库存
            GoodsSku::where('id', $post['goods_sku_id'])->update(['sku_stock' => $skuNum]);
            $save = $this->model->save($post);
        } catch (\Exception $e) {
            return $this->error('保存失败:' . $e->getMessage());
        }
//        $save ? $this->success('保存成功') : $this->error('保存失败');
        return redirect('/admin/store.PinkConfig/index');
    }


}